
#include "mymainwindow.h"
#include "openglframe.h"
#include "typedefs.h"

#include <iostream>

using namespace std;

/// Constructor.
MyMainWindow::MyMainWindow(QMainWindow *parent): QMainWindow(parent) {
   
   // Make sure the default opengl format includes stencil buffer and alpha.
   QGLFormat fmt;
   fmt.setAlpha(true);
   fmt.setStencil(true);
   QGLFormat::setDefaultFormat(fmt);
   
   // Executes the method which builds the remaining widgets of the window.
   // (Inherited from Ui::MainWindow)
   setupUi (this);
   
   timer = new QTimer(this);
   
   connect(actionZoomIn, SIGNAL(triggered()), this->glFrame, SLOT(zoomIn()));
   connect(actionZoomOut, SIGNAL(triggered()), this->glFrame, SLOT(zoomOut()));
   connect(actionZoomAll, SIGNAL(triggered()), this->glFrame, SLOT(zoomAll()));
   
   //~ // groupping exclusives actions 
   meshrenderGroup = new QActionGroup(this);
   meshrenderGroup->addAction(actionRenderPoints);
   meshrenderGroup->addAction(actionRenderWireframe);
   meshrenderGroup->addAction(actionRenderHiddenLines);
   meshrenderGroup->addAction(actionRenderFlatLines);
   meshrenderGroup->addAction(actionRenderFlat);
   meshrenderGroup->addAction(actionRenderSmooth);
}

// Tries to load file if filename not empty.
void MyMainWindow::open(QString filename) {
   
   if (filename != "") {
      QFileInfo fi(filename);
      QString extension = fi.suffix();
      
      if (extension != "off" and extension != "ply") {
         QString errorMsgFormat = "Error encountered while opening file:\n\"%1\"\n\nError details: The \"%2\" file extension is not supported. \nYou must open < off, ply > format models.";
         QMessageBox::critical(this, tr("Opening Error"), errorMsgFormat.arg(filename, extension));
         return;
      }
      
      if (not glFrame->loadShape(filename)) {
         QString errorMsgFormat = "Error encountered while opening file:\n\"%1\"\n\nError details: The \"%2\" format is incorrect";
         QMessageBox::critical(this, tr("Opening Error"), errorMsgFormat.arg(filename, extension));
         return;
      }
      
      filenamemodel = filename;
      this->setWindowTitle(" qtMeshViewer - " + filenamemodel);
   }
}


/// Automatically bound to the actionOpen menu item of the main window 
/// by the designer mechanism. Pops up a file open dialog for selecting
/// an "off" file.
void MyMainWindow::on_actionOpen_triggered() 
{
    // Asks for a file name to open.
    QString filename = QFileDialog::getOpenFileName (
                      this, 
                      tr("Choose a file"),
                      ".",
                      tr("Off meshes (*.off)"));
    
    //~ // Tries to load file if filename not empty.
    if (filename != "") {
      open(filename);
      glFrame->updateGL();
    }
}

void MyMainWindow::on_actionRenderPoints_triggered() {
   glFrame->meshRenderMode(POINTS);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderWireframe_triggered() {
   glFrame->meshRenderMode(WIREFRAME);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderFlat_triggered() {
   glFrame->meshRenderMode(FLAT);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderFlatLines_triggered() {
   glFrame->meshRenderMode(FLATLINES);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderHiddenLines_triggered() {
   glFrame->meshRenderMode(HIDDENLINES);
   glFrame->updateGL();
}

void MyMainWindow::on_actionRenderSmooth_triggered() {
   glFrame->meshRenderMode(SMOOTH);
   glFrame->updateGL();
}

void MyMainWindow::on_actionLightOnOff_triggered() {
   if (actionLightOnOff->isChecked())
      actionLightOnOff->setIcon(QIcon(QString::fromUtf8("icons/lighton.png")));
   else 
      actionLightOnOff->setIcon(QIcon(QString::fromUtf8("icons/lightoff.png")));
   glFrame->lightingOnOff(actionLightOnOff->isChecked());
   glFrame->updateGL();
}

void MyMainWindow::on_actionProjection_triggered() {
   
   if (glFrame->isProjectionOrtho()) {
      const QString qstring("Perspective");
      actionProjection->setText(qstring);
      actionProjection->setIcon(QIcon(QString::fromUtf8("icons/perspective.png")));
      glFrame->orthographic(false);
   }
   else {
      const QString qstring("Orthographic");
      actionProjection->setText(qstring);
      actionProjection->setIcon(QIcon(QString::fromUtf8("icons/orthographic.png")));
      glFrame->orthographic(true);
   }
   
   glFrame->setGLview();
   glFrame->updateGL();
}

void MyMainWindow::on_actionViewModel_triggered() {
   
   if (actionViewModel->isChecked())
      glFrame->viewItemOn(MODELDEF);
   else 
      glFrame->viewItemOff(MODELDEF);
   glFrame->updateGL();
}

void MyMainWindow::on_actionViewFloor_triggered() {
   
   if (actionViewFloor->isChecked())
      glFrame->viewItemOn(FLOOR);
   else 
      glFrame->viewItemOff(FLOOR);
   glFrame->updateGL();
}

void MyMainWindow::on_actionViewInfo_triggered() {
   
   if (actionViewInfo->isChecked())
      glFrame->viewItemOn(INFO);
   else 
      glFrame->viewItemOff(INFO);
   glFrame->updateGL();
}
